package com.appkubes.mall.admin.application.utm.utm.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.utm.utm.entity.vo.UtmEditVO;
import com.appkubes.mall.admin.application.utm.utm.entity.vo.UtmGetVO;
import com.appkubes.mall.admin.application.utm.utm.entity.vo.UtmSourceGetVO;
import com.appkubes.mall.admin.application.utm.utm.entity.vo.UtmStateVO;
import com.appkubes.mall.core.controller.BaseMallUtmSourceController;
import com.appkubes.mall.core.entity.MallUtmSource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@Api("渠道模块")
@RestController
@RequestMapping("/utm/source")
public class UtmSourceController extends BaseMallUtmSourceController {

    @ApiOperation("渠道列表")
    @PostMapping("/getList")
    public Response<PageInfo<MallUtmSource>> getList(@ParamValid UtmSourceGetVO utmSourceGetVO) {
        QueryWrapper<MallUtmSource> queryWrapper = new QueryWrapper();

        MallUtmSource entity = new MallUtmSource();
        entity.setSourceType(utmSourceGetVO.getSourceType());

        queryWrapper.setEntity(entity);
        queryWrapper.orderByDesc("create_time");
        IPage<MallUtmSource> page = new Page<>(utmSourceGetVO.getPage(), utmSourceGetVO.getSize());
        IPage<MallUtmSource> mallUtmSources = this.getBaseMapper().selectPage(page, queryWrapper);

        return Response.success(mallUtmSources);
    }

    @ApiOperation("开启渠道")
    @PostMapping("/setState")
    public Response setState(@ParamValid UtmStateVO utmStateVO) {
        MallUtmSource entity = new MallUtmSource();
        entity.setState(utmStateVO.getState());

        QueryWrapper<MallUtmSource> query = new QueryWrapper<>();
        List<String> keys = Arrays.asList(utmStateVO.getKeys().split(","));
        query.in("utm_key", keys);

        Boolean flag = this.update(entity, query);
        return Response.success(flag);
    }


    @ApiOperation("获取渠道详情")
    @PostMapping("get")
    public Response get(@ParamValid UtmGetVO utmGetVO) {
        MallUtmSource utmSource = (MallUtmSource) this.getById(utmGetVO.getUtmKey());
        return Response.success(utmSource);
    }

    @ApiOperation("修改渠道信息")
    @PostMapping("/save")
    public Response modify(@ParamValid UtmEditVO utmEditVO) {
        MallUtmSource UtmSource = new MallUtmSource();
        BeanUtils.copyProperties(utmEditVO, UtmSource);
        return Response.success(this.saveOrUpdate(UtmSource));
    }

    @Override
    public Boolean checkDelByIdCondition(Serializable id) {
        return true;
    }
}
